package com.wei.czz.framework.admin.service.impl;

import com.wei.czz.common.enums.CommonEnum;
import com.wei.czz.common.utils.SecurityUtils;
import com.wei.czz.framework.common.service.AesService;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wei.czz.framework.admin.dao.UserContactFlowDao;
import com.wei.czz.framework.admin.entity.UserContactFlowEntity;
import com.wei.czz.framework.admin.service.UserContactFlowService;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2023-03-30 16:44:48
 * className: UserContactFlowServiceImpl 用户联系方式记录请求服务接口实现类
 * version: 1.0
 * description:
 */
@Service("userContactFlowService")
@AllArgsConstructor
public class UserContactFlowServiceImpl extends ServiceImpl<UserContactFlowDao, UserContactFlowEntity>
        implements UserContactFlowService {

    private static final Logger log = LoggerFactory.getLogger(UserContactFlowServiceImpl.class);

    private final AesService aesService;

    @Override
    public void saveUserContactFlow(Long userId, Integer type, Integer mode, String contact) {
        // 获取操作用户
        Long optUserId = SecurityUtils.getUserId();

        // 数据加密
        contact = aesService.encryptToHex(contact);

        Date date = new Date();

        UserContactFlowEntity userContactFlow = new UserContactFlowEntity();
        userContactFlow.setUserId(userId)
                .setType(type)
                .setMode(mode)
                .setContact(contact)
                .setDeleteFlag(CommonEnum.ZERO.getValue())
                .setDeleteTime(date)
                .setCreateTime(date)
                .setCreateUser(optUserId);
        // 插入数据
        int count = baseMapper.insert(userContactFlow);
        log.info("用户联系方式记录添加完成。count={}", count);
    }

    @Override
    public void saveOrEdit(UserContactFlowEntity userContactFlow) {
        Long id = userContactFlow.getId();
        if (Objects.isNull(id)) {

            int count = baseMapper.insert(userContactFlow);
            log.info("插入用户联系方式记录完成。count={}", count);
        } else {
            int count = baseMapper.updateById(userContactFlow);
            log.info("修改用户联系方式记录完成。count={}", count);
        }
    }

    @Transactional
    @Override
    public void batchSave(List<UserContactFlowEntity> userContactFlowList) {
        boolean bool = this.saveBatch(userContactFlowList);
        log.info("批量保存用户联系方式记录完成。bool={}", bool);
    }

}